/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import control.crudMovie;
import control.crudPerson;
import entidad.Actor;
import entidad.Movie;
import entidad.Person;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;

/**
 *
 * @author Stiv
 */
public class Busqueda extends javax.swing.JPanel {
    
    public DefaultListModel dlmlista = new DefaultListModel();
    
    
    Movie f = new Movie();
    Person p = new Person();

    /**
     * Creates new form Busqueda
     */
    public Busqueda() {
        initComponents();
        inicializar();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public void inicializar(){
        InfoBT.setVisible(false);
        InfoActorBT.setVisible(false);
        Lista.setModel(dlmlista);
        ArrayList<Movie> inicial = new ArrayList();
        inicial = crudMovie.AllPeliculas();
        for(Movie u: inicial){
            dlmlista.addElement(u);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        PeliculaTF = new javax.swing.JTextField();
        ActorTF = new javax.swing.JTextField();
        DirectorTF = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Lista = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        GeneroTF = new javax.swing.JTextField();
        ArtistaTF = new javax.swing.JTextField();
        InfoBT = new javax.swing.JButton();
        InfoActorBT = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Busqueda de pelicula"));

        jLabel1.setText("Pelicula");

        jLabel2.setText("Actor");

        jLabel3.setText("Director");

        PeliculaTF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                PeliculaTFMouseReleased(evt);
            }
        });
        PeliculaTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                PeliculaTFKeyReleased(evt);
            }
        });

        ActorTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ActorTFKeyReleased(evt);
            }
        });

        DirectorTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                DirectorTFKeyReleased(evt);
            }
        });

        Lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ListaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Lista);

        jLabel4.setText("Genero");

        jLabel5.setText("Artista");

        GeneroTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                GeneroTFKeyReleased(evt);
            }
        });

        ArtistaTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ArtistaTFKeyReleased(evt);
            }
        });

        InfoBT.setText("Detalles Pelicula");
        InfoBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InfoBTActionPerformed(evt);
            }
        });

        InfoActorBT.setText("Detalles Persona");
        InfoActorBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InfoActorBTActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(PeliculaTF)
                    .addComponent(ActorTF)
                    .addComponent(DirectorTF, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                    .addComponent(GeneroTF)
                    .addComponent(ArtistaTF))
                .addGap(87, 87, 87)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(InfoActorBT)
                        .addGap(18, 18, 18)
                        .addComponent(InfoBT)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(InfoBT)
                            .addComponent(InfoActorBT))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(PeliculaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(ActorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(69, 69, 69)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(DirectorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(GeneroTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(ArtistaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ActorTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ActorTFKeyReleased
        // TODO add your handling code here:
        dlmlista.clear();
        
        ArrayList<Person> actores = new ArrayList();
        actores = crudPerson.TraerActores(ActorTF.getText());
        for(Person c: actores){
        dlmlista.addElement(c);
        }

        
    }//GEN-LAST:event_ActorTFKeyReleased

    private void PeliculaTFMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PeliculaTFMouseReleased
        // TODO add your handling code here:
        
    }//GEN-LAST:event_PeliculaTFMouseReleased

    private void InfoBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InfoBTActionPerformed
        
        this.setVisible(false);
        InfoPelicula info = new InfoPelicula();
        this.getParent().add(info);
        try{
            info.traerMovie(f);
        }
        catch(NullPointerException npe){
            
        }
        info.setVisible(true);
        
        
    }//GEN-LAST:event_InfoBTActionPerformed

    private void PeliculaTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PeliculaTFKeyReleased
        // TODO add your handling code here:
        dlmlista.clear();
        
        ArrayList<Movie> peliculas = new ArrayList();
        peliculas = crudMovie.TraerPeliculas(PeliculaTF.getText());
        for(Movie c: peliculas){
        dlmlista.addElement(c);
        }
    }//GEN-LAST:event_PeliculaTFKeyReleased

    private void DirectorTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_DirectorTFKeyReleased
        dlmlista.clear();
        
        ArrayList<Person> directores = new ArrayList();
        directores = crudPerson.TraerDirectores(DirectorTF.getText());
        for(Person c: directores){
        dlmlista.addElement(c);
        }
    }//GEN-LAST:event_DirectorTFKeyReleased

    private void ArtistaTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ArtistaTFKeyReleased
        dlmlista.clear();
        
        ArrayList<Person> artistas = new ArrayList();
        artistas = crudPerson.TraerArtistas(ArtistaTF.getText());
        for(Person c: artistas){
        dlmlista.addElement(c);
        }
    }//GEN-LAST:event_ArtistaTFKeyReleased

    private void GeneroTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_GeneroTFKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER){
             dlmlista.clear();
             
             ArrayList<Movie> peliculas = new ArrayList();
             peliculas = crudMovie.PeliculasPorGenero(GeneroTF.getText());
             for(Movie u: peliculas){
                 dlmlista.addElement(u);
             }
        }
        
    }//GEN-LAST:event_GeneroTFKeyReleased

    
    public Movie PeliculaLista(){
        Movie p = (Movie)Lista.getSelectedValue();
        return p;
    }
            
    private void ListaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaMouseClicked
        InfoBT.setVisible(true);
        InfoActorBT.setVisible(true);
        try{
        f = (Movie)Lista.getSelectedValue();            
        }
        catch(ClassCastException e){
            
        }
        try{
            p = (Person)Lista.getSelectedValue();
        }
        catch(ClassCastException e){
            
        }
    }//GEN-LAST:event_ListaMouseClicked

    private void InfoActorBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InfoActorBTActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        InfoActor infoA = new InfoActor();
        this.getParent().add(infoA);
        infoA.traerPersona(p);
        infoA.setVisible(true);
    }//GEN-LAST:event_InfoActorBTActionPerformed

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ActorTF;
    private javax.swing.JTextField ArtistaTF;
    private javax.swing.JTextField DirectorTF;
    private javax.swing.JTextField GeneroTF;
    private javax.swing.JButton InfoActorBT;
    private javax.swing.JButton InfoBT;
    private javax.swing.JList Lista;
    private javax.swing.JTextField PeliculaTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
